//mediator pattern interface
//andycai.sc@gmail.com 2012-04-13

#ifndef GMEDIATORCOLLE_H
#define GMEDIATORCOLLE_H

#include "GDomainObject.h"

//---------------define GMediator interface-------------------

class GColleague;
class GMediator
{
public:
	virtual ~GMediator() {}

	//this interface use update mediator or colleague status
	virtual void change(GDomainEvent* changeEvent, GColleague* colleague) = 0;

protected:
	GMediator() {}
};

//------------------define GColleague interface-----------------

class GColleague : public GDataReceiver
{
public:
	virtual ~GColleague() {}

	void setMediator(GMediator* mediator)
	{
		m_mediator = mediator;
	}

protected:
	GColleague(GMediator* mediator = NULL)
	{
		m_mediator = mediator;
	}

	//this method use to notify mediator it changed
	virtual void changed(GDomainEvent* changeEvent)
	{
		//assert(m_mediator == NULL);
		m_mediator->change(changeEvent, this);
	}

	virtual void process(GDomainEvent* dEvent, GDomainObject* dObject)
	{
		changed(dEvent);
	}

	GMediator* m_mediator;
};

template <typename T = GDomainObject>
class GDomainColleague : public GColleague
{
public:
	GDomainColleague(T* domainObj = NULL, GMediator* mediator = NULL) :
	GColleague(mediator), m_domainObj(domainObj)
	{
	}
	virtual ~GDomainColleague() {}

	T* operator->() const
	{
		return m_domainObj;
	}

	T* get() const
	{
		return m_domainObj;
	}

protected:
	T* m_domainObj;
};



#endif
